<?php
//退出申请
$param = file_get_contents('php://input');
// $param = file_get_contents('notify1.txt');
file_put_contents('refund_callback.json', $param);
$url = file_get_contents('notify_url.txt');
$param = json_decode($param, true);

if (!empty($param['msg'])) {

    $msg = $param['msg'];
    $msg = json_decode($msg, true);

    $out_refund_no = 'TK' . time() . uniqid();

    if (!empty($msg['cp_extra'])) {
        $orderId = json_decode($msg['cp_extra'])->orderId;
        echo $orderId;
        send_post($url, arrayToXml([
            "status" => "refund",
            "out_refund_no" => $out_refund_no,
            "cp_extra" => $orderId
        ]));

    }

    echo json_encode([
        'err_no' => 0,
        'err_tips' => 'success',
        "data" => [
            "out_refund_no" => $out_refund_no,
            "order_entry_schema" => [
                "path" => "ext://microapp-trade-plugin/refund-detail"//退款详情路径
            ],
            // "notify_url"=>"//
        ]

    ]);


    // var_dump($msg['goods'][0]['goods_id']);
    // exit;

} else {
    //
    echo json_encode([
        'err_no' => 1,
        'err_tips' => 'success',

    ]);
}
exit;
function arrayToXml($arr)
{

    $xml = "<xml>";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<$key>$val</$key>";
        } else
            $xml .= "<$key><![CDATA[$val]]></$key>";
    }
    $xml .= "</xml>";
    return $xml;
}
function send_post($url, $post_data)
{
    // $postdata = http_build_query($post_data);
    file_put_contents('xml.txt', $post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/xml',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
